package org.nio;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * @author wind
 * @create 2025-01-06 10:03
 */
public class SplitByteDemo {
    public static void main(String[] args) {
        ByteBuffer source = ByteBuffer.allocate(32);
        //                     11            24
        source.put("Hello,world\nI'm zhangsan\nHo".getBytes());
        split(source);

        //source.put("w are you?\nhaha!\n".getBytes());
        //split(source);
    }

    public static void split(ByteBuffer source) {
        source.flip();
        for(int i=0;i<source.limit(); i++){
            if(source.get(i) == '\n'){
                //System.out.println(i);
                //System.out.println(source.position());
                int length = i+1-source.position();
                ByteBuffer target = ByteBuffer.allocate(length);
                for(int j=0;j<length;j++){
                    target.put(source.get());
                }
                ByteBufferUtil.debug(target);
            }
        }
        source.compact();
    }
}
